// pages/getTicket/getTicket.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "1",
    mobile: "",
    number: "1",
    oid: "11",
    product_id: "6000",
    name: "",
    bg: "../image/send-bg.png",
    content: "../image/content.png",
    isOnload: false,
    insertMobile: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function(options) {
    let that = this;
    wx.getStorage({
      key: 'uid',
      success: function(res) {
        that.setData({
          uid: res.data,
          isOnload: true
        })
        wx.request({
          url: 'https://api.bshlsqs.com/user_center/index',
          data: {
            uid: res.data
          },
          success(res) {
            console.log(res)
            var mobile = res.data.data.info.mobile
            that.setData({
              mobile: mobile
            })
          }
        })
      },
    })
    console.log(options)
    var oid = options.oid;
    var number = options.number;
    var product_id = options.product_id;
    var name = decodeURI(options.name)
    var cate_name = decodeURI(options.cate_name)

    this.setData({
      oid: oid,
      number: number,
      product_id: product_id,
      name: name,
      cate_name: cate_name
    })
  },

  // 输入手机号码
  mobileInput(e) {
    var val = e.detail.value
    this.setData({
      insertMobile: val
    })
  },

  // 领取
  handleSubmit() {
    let that = this;
    var data = {
      number: that.data.number,
      product_id: that.data.product_id,
      oid: that.data.oid,
    }
    if (that.data.mobile) {
      data['mobile'] = that.data.mobile
    } else {
      data['mobile'] = that.data.insertMobile
    }
    if (that.data.uid) {
      data['uid'] = that.data.uid
    }
    wx.request({
      url: 'https://api.bshlsqs.com/order/receiveTicket',
      data: data,
      success(res) {
        console.log(res)
        if (res.data.code == 0) {

          wx.showToast({
            title: '领取成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }, 2 * 1000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})